﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class uc_SinhVien : DevExpress.XtraEditors.XtraUserControl
    {
        #region khai bao bien
        TextMessageBox box = new TextMessageBox();
        DataTable dt;
        #endregion

        #region Loadform
        public uc_SinhVien()
        {
            InitializeComponent();
        }
        SinhVien_BLL sv_bll = new SinhVien_BLL();
        private void uc_SinhVien_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region cac chuc nang
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmSinhVien frm = new frmSinhVien();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.FocusedRowHandle > -1)
            {
                frmSinhVien frm = new frmSinhVien();
                frm.maDocGia = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colMaDocGi).ToString();
                if (frm.ShowDialog() == DialogResult.OK)
                    LoadData();
            }
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.FocusedRowHandle > -1)
            {
                SinhVien_Public sv=new SinhVien_Public();
                string ma=grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colMaDocGi).ToString();
                sv.MaDocGia=ma;
                string ten=grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle,colHoVaTen).ToString();
                dt=sv_bll.TaoBang(box.sqlCheckExist("DocGia","SoThe",ma));
                if (dt.Rows.Count > 0)
                {
                    XtraMessageBox.Show(box.NotDelete(ma), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (XtraMessageBox.Show(box.Delete_Confirm(ma, ten), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            sv_bll.Delete(sv);
                            LoadData();
                        }
                        catch(Exception)
                        {
                            XtraMessageBox.Show(box.DeleteFail,box.Caption_Fail,MessageBoxButtons.OK,MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }
        #endregion

        #region ham viet them

        private void LoadData()
        {
            grd_Data.DataSource = sv_bll.LayDuLieu();
        }

        #endregion
    }
}
